
import { ref } from 'vue';
import { defineStore } from 'pinia';

export const useThemeStore = defineStore('theme', () => {
  const isNightMode = ref(false); // 使用 ref 创建响应式状态

  const initialize = () => {
    const storedMode = localStorage.getItem('theme');
    // 对 storedMode 进行非空判断
    if (storedMode) {
        const parsedMode = JSON.parse(storedMode);
        if (parsedMode.isNightMode) {
            document.documentElement.className += ' night-mode';
        }
    }
  };

  const toggleNightMode = () => {
    isNightMode.value = !isNightMode.value; // 切换状态
    if (isNightMode.value) {
      document.documentElement.classList.toggle('night-mode');
    } else{
      document.documentElement.classList.remove('night-mode');
    }
  };

  return { isNightMode, initialize, toggleNightMode }; // 返回状态和方法
} ,{
  persist: true
}

);
